<?php
/**
 * @version $Id: thumbnail_images.class.php 15 2010-05-13 17:28:34Z vladzimirs@gmail.com $
 * @package UnoCart
 * @copyright Авторские права (C) 2009-2010 UnoCart team & (C) 2007-2010 Joostina team &. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * UnoCart! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл help/copyright.php.
 */
/*
+----------------------------------------------------------------------+
| ThumbnaiI Image v.: 0.1 (05.12.2006)                                 |
+----------------------------------------------------------------------+
| Generate thumbnail images                                            |
+----------------------------------------------------------------------+
| Author: Stigmat <multzone@inbox.ru>                                  |
+----------------------------------------------------------------------+
*/

class thumbnail_images
{

    // get
    var $PathImgOld;
    var $PathImgNew;
    var $NewWidth;
    var $NewHeight;

    // tmp
    var $mime;

    function imagejpeg_new($NewImg, $path_img)
    {
        if ($this->mime == 'image/jpeg' or $this->mime == 'image/pjpeg')
            imagejpeg($NewImg, $path_img, 100);
        elseif ($this->mime == 'image/gif')
            imagegif($NewImg, $path_img);
        elseif ($this->mime == 'image/png')
            imagepng($NewImg, $path_img);
        else
            return (false);
        return (true);
    }

    function imagecreatefromjpeg_new($path_img)
    {
        if ($this->mime == 'image/jpeg' or $this->mime == 'image/pjpeg')
            $OldImg = imagecreatefromjpeg($path_img);
        elseif ($this->mime == 'image/gif')
            $OldImg = imagecreatefromgif($path_img);
        elseif ($this->mime == 'image/png') {
            $OldImg = imagecreatefrompng($path_img);
        } else
            return (false);
        return ($OldImg);
    }

    function create_thumbnail_images()
    {
        ini_set("memory_limit", "120M");
        $PathImgOld = $this->PathImgOld;
        $PathImgNew = $this->PathImgNew;
        $NewWidth = intval($this->NewWidth);
        $NewHeight = intval($this->NewHeight);

        $Oldsize = @getimagesize($PathImgOld);
        $this->mime = $Oldsize['mime'];
        $OldWidth = $Oldsize[0];
        $OldHeight = $Oldsize[1];

        if ($NewHeight == '0' and $NewWidth != '') {
            $NewHeight = ceil(($OldHeight * $NewWidth) / $OldWidth);
        } elseif ($NewWidth == '0' and $NewHeight != '') {
            $NewWidth = ceil(($OldWidth * $NewHeight) / $OldHeight);
        } elseif ($NewHeight == '0' and $NewWidth == '0') {
            return (false);
        }

        $OldHeight_castr = ceil(($OldWidth * $NewHeight) / $NewWidth);
        $castr_bottom = ($OldHeight - $OldHeight_castr) / 2;

        $OldWidth_castr = ceil(($OldHeight * $NewWidth) / $NewHeight);
        $castr_right = ($OldWidth - $OldWidth_castr) / 2;

        if ($castr_bottom > 0) {
            $OldWidth_castr = $OldWidth;
            $castr_right = 0;
        } elseif ($castr_right > 0) {
            $OldHeight_castr = $OldHeight;
            $castr_bottom = 0;
        } else {
            $OldWidth_castr = $OldWidth;
            $OldHeight_castr = $OldHeight;
            $castr_right = 0;
            $castr_bottom = 0;
        }

        $OldImg = $this->imagecreatefromjpeg_new($PathImgOld);
        if ($OldImg) {
            $NewImg_castr = imagecreatetruecolor($OldWidth_castr, $OldHeight_castr);
            if ($NewImg_castr) {
                if ($this->mime == 'image/png') {
                    imagealphablending($NewImg_castr, false);
                    imagesavealpha($NewImg_castr, true);
                } else
                    if ($this->mime == 'image/gif') {
                        $trnprt_indx = imagecolortransparent($OldImg);
                        $trnprt_color = imagecolorsforindex($OldImg, $trnprt_indx);
                        imagepalettecopy($OldImg, $NewImg_castr);
                        imagefill($NewImg_castr, 0, 0, $trnprt_color);
                        imagecolortransparent($NewImg_castr, $trnprt_color);
                        imagetruecolortopalette($NewImg_castr, true, 256);
                    }
                imagecopyresampled($NewImg_castr, $OldImg, 0, 0, $castr_right, $castr_bottom, $OldWidth_castr,
                    $OldHeight_castr, $OldWidth_castr, $OldHeight_castr);
                $NewImg = imagecreatetruecolor($NewWidth, $NewHeight);
                // Taras add for work with gif transparent
                if ($NewImg) {
                    if ($this->mime == 'image/png') {
                        imagealphablending($NewImg, false);
                        imagesavealpha($NewImg, true);
                    } else
                        if ($this->mime == 'image/gif') {
                            if ($trnprt_indx > 0 && $trnprt_indx < imagecolorstotal($OldImg)) {
                                imagepalettecopy($OldImg, $NewImg);
                                imagefill($NewImg, 0, 0, $trnprt_color);
                                imagecolortransparent($NewImg, $trnprt_color);
                                imagetruecolortopalette($NewImg, true, 256);
                            }
                        }
                    // End Taras add
                    imagecopyresampled($NewImg, $NewImg_castr, 0, 0, 0, 0, $NewWidth, $NewHeight, $OldWidth_castr,
                        $OldHeight_castr);
                    imagedestroy($NewImg_castr);
                    imagedestroy($OldImg);
                    if (!$this->imagejpeg_new($NewImg, $PathImgNew))
                        return (false);
                    imagedestroy($NewImg);
                }
            }
        } else {
            return (false);
        }

        return (true);
    }

}

?>